package com.ohuang.test_compose.page

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier

@Composable
fun RemberTestPage() {
    var name = remember {
        mutableStateOf("aaa")
    }
    Surface(color = MaterialTheme.colors.background) {

        Column {
            Text(text = "点击改变Name", Modifier.clickable { name.value="name${System.currentTimeMillis()%100}" })
            testRemember(name = name.value)
            testRememberForKey(name = name.value)
            testRememberSaveable(name=name.value)
            testRememberSaveableFoeKey(name=name.value)
            if (name.value=="aaa"){ // 虽然调用了相同的代码,但和直接调用testRemember(name = name.value)效果不一样
                testRemember(name = name.value)
            }else{
                testRemember(name = name.value)
            }
        }

    }
}

@Composable
fun testRemember(name:String){
    var sate = remember {
        mutableStateOf("未点击")
    }
    Text("点击改变状态 name=${name}  state=${sate.value}",Modifier.clickable {
        sate.value="点击了"
    })
}
@Composable
fun testRememberForKey(name:String){
    var sate = remember(name) {
        mutableStateOf("未点击")
    }
    Text("点击改变状态 name=${name}  state=${sate.value}",Modifier.clickable {
        sate.value="点击了"
    })
}


@Composable
fun testRememberSaveable(name:String){
    var sate = rememberSaveable {
        mutableStateOf("未点击")
    }
    Text("点击改变状态 name=${name}  state=${sate.value}",Modifier.clickable {
        sate.value="点击了"
    })
}
@Composable
fun testRememberSaveableFoeKey(name:String){
    var sate = rememberSaveable(name) {
        mutableStateOf("未点击")
    }
    Text("点击改变状态 name=${name}  state=${sate.value}",Modifier.clickable {
        sate.value="点击了"
    })
}